﻿<%@ WebHandler Language="C#" Class="ZSScrapinfoReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 装配试车厂报废品统计报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.11.24
 **/

public class ZSScrapinfoReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"
SELECT csc.zfpdbh,m.mfgordername,u.attributevalue,cm.productname,p.description,cm.productbatchno,ism.containerno,
       csc.fecod,DECODE(csc.zbfljlx,'01','零件报废','02','组件报废','03','组件中单件报废',csc.zbfljlx) zbfljlx,
       getempfullname(csc.employeeid) worker,csc.zzfpply,csc.zzrdw,ii.lossqty,csc.createdate,csc.zzttxt
FROM containerstepscrapinfo csc
LEFT JOIN issueerpmaterialscrapinfo ii ON csc.qtyhistorydetailsid=ii.issueerpmaterialscrapinfoid
LEFT JOIN issueerpmaterial ism ON ii.issueerpmaterialid=ism.issueerpmaterialid
LEFT JOIN partmenuinfo pm ON ism.partmenuinfoid=pm.partmenuinfoid
LEFT JOIN product p ON pm.productid=p.productid
LEFT JOIN collecterpmaterial cm ON cm.collecterpmaterialid=ism.collecterpmaterialid
LEFT JOIN container c ON c.containerid=csc.containerid
LEFT JOIN mfgorder m ON c.mfgorderid=m.mfgorderid
LEFT JOIN Userattribute u ON m.mfgorderid=u.parentid AND u.userattributename='发动机'
LEFT JOIN factory f ON f.factoryid = csc.factoryid
WHERE ism.ischange=0 AND f.factoryname = '装配试车厂'
";
        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND m.mfgordername = '{0}' ", jObject["mfgordername"].ToString());
        }

        //发动机号
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND u.attributevalue = '{0}' ", jObject["engineno"].ToString());
        }

        //件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cm.productname = '{0}' ", jObject["productname"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        dt.Columns["zfpdbh"].Caption = "废品单号";
        dt.Columns["mfgordername"].Caption = "生产订单号";
        dt.Columns["attributevalue"].Caption = "发动机号";
        dt.Columns["productname"].Caption = "零件号";
        dt.Columns["description"].Caption = "零件名称";
        dt.Columns["productbatchno"].Caption = "批次号";
        dt.Columns["containerno"].Caption = "任务单名称";
        dt.Columns["fecod"].Caption = "报废原因";
        dt.Columns["zbfljlx"].Caption = "废品类型";
        dt.Columns["worker"].Caption = "报废人";
        dt.Columns["zzfpply"].Caption = "生产单位";
        dt.Columns["zzrdw"].Caption = "责任单位";
        dt.Columns["lossqty"].Caption = "报废数量";
        dt.Columns["createdate"].Caption = "报废时间";
        dt.Columns["zzttxt"].Caption = "报废情况";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "报废品统计报表");
    }
}